<?php

namespace app\models;


/**
 * 用户的收货地址
 * Class Address
 * @package app\models
 */
class Address
{
    /**
     * 新增或者修改收货地址
     * @param $userId
     * @param string|null $addressId if is null, then add a new address
     * @param $name
     * @param $phone
     * @param $provinceName
     * @param $cityName
     * @param $regionName
     * @param $address
     * @param $remark
     * @return false|null|string
     * @throws \yii\db\Exception
     */
    public static function saveAddress($userId, $addressId, $name, $phone, $provinceName, $cityName, $regionName, $address, $remark)
    {

        $cmd = \Yii::$app->db->createCommand('CALL p_save_user_address(:in_user_id, :in_address_id, :in_name, :in_phone, :in_province_name, :in_city_name, :in_region_name, :in_address, :in_remark,@oid)');
        $cmd->bindValue(':in_user_id', $userId);
        $cmd->bindValue(':in_address_id', $addressId);
        $cmd->bindValue(':in_name', $name);
        $cmd->bindValue(':in_phone', $phone);
        $cmd->bindValue(':in_province_name', $provinceName);
        $cmd->bindValue(':in_city_name', $cityName);
        $cmd->bindValue(':in_region_name', $regionName);
        $cmd->bindValue(':in_address', $address);
        $cmd->bindValue(':in_remark', $remark);

        $cmd->execute();

        $cmd = \Yii::$app->db->createCommand('SELECT @oid');
        $oid = $cmd->queryScalar();

        self::setDefault($userId, $oid);

        return $oid;
    }

    public static function getDefault($userId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_address WHERE is_enabled=1 AND is_default=1 AND user_id=:user_id');
        $cmd->bindValue(':user_id', $userId);

        $item = $cmd->queryOne();
        if ($item != false) {
            return $item;
        } else {
            return null;
        }
    }

    /**
     * 获取用户所有收货地址列表
     * @param $userId
     * @return array|null
     */
    public static function getUserAllAddress($userId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_address WHERE is_enabled=1 AND user_id=:user_id ORDER BY create_datetime DESC');
        $cmd->bindValue(':user_id', $userId);

        $items = $cmd->queryAll();
        return $items;
    }

    public static function getDetail($addressId)
    {
        if ($addressId == null || $addressId == '') return null;

        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_address WHERE is_enabled=1 AND id=:addressId ');
        $cmd->bindValue(':addressId', $addressId);

        $item = $cmd->queryOne();

        if ($item != false) {
            return $item;
        } else {
            return null;
        }
    }

    public static function setDefault($userId, $addressId)
    {
        $sql = "update t_address set is_default=0 where id <>:addressId and user_id=:userId";
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':addressId', $addressId);
        $cmd->bindValue(':userId', $userId);
        $rt = $cmd->execute();

        $sql2 = "update t_address set is_default=1 where id=:addressId";
        $cmd2 = \Yii::$app->db->createCommand($sql2);
        $cmd2->bindValue(':addressId', $addressId);
        $rt2 = $cmd2->execute();

        if ($rt2 > 0) {
            return 0;
        } else {
            return 1;
        }
    }

    public static function deleteAddress($userId, $addressId)
    {
        $cmd = \Yii::$app->db->createCommand('call p_delete_user_address(:in_address_id, :in_user_id)');
        $cmd->bindValue(':in_address_id', $addressId);
        $cmd->bindValue(':in_user_id', $userId);
        $rt = $cmd->execute();

        if ($rt > 0) {
            return 0;
        } else {
            return 1;
        }
    }
}